<template>
  <div>
    <el-card>
      <el-form :inline="true" ref="searchForm" :model="searchForm">
        <el-form-item label="车牌号">
          <el-input
            v-model="searchForm.carNumber"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="出入状态:">
          <el-select v-model="searchForm.status" clearable>
            <el-option label="外出" value="0"> </el-option>
            <el-option label="进入" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起止时间" prop="time">
           <el-date-picker
            v-model="searchForm.time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd hh:mm:ss" value-format="yyyy-MM-dd hh:mm:ss">
            </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getData()">查询</el-button>
          <el-button
          v-if="userInfo.type==='0'"
            type="primary"
            @click="openDialog('添加车辆出入信息', { a: '1' })"
            plain
            >添加</el-button
          >
          <el-button v-if="userInfo.type==='0' && searchForm.time" type="primary">
            <a
              style="color: white"
              :href="
                'http://localhost:8081/VehicleManagement/record/exportRecordListInfo?carNumber=' +
                searchForm.carNumber+'&status='+searchForm.status+'&timeStart='+searchForm.time[0]+'&timeEnd='+searchForm.time[1]
              "
              >导出</a
            >
          </el-button>
          <el-button type="primary" v-if="!searchForm.time">
            <a
              style="color: white"
              :href="
                'http://localhost:8081/VehicleManagement/record/exportRecordListInfo?carNumber=' +
                searchForm.carNumber+'&status='+searchForm.status
              "
              >导出</a
            >
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 数据列表-->
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
        
      >
        <el-table-column
          prop="carNumber"
          label="车牌号"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="parkNumber"
          label="车位号"
          min-width="120"
        ></el-table-column>
        <el-table-column prop="status" label="进出状态" min-width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.status === '0'">外出</span>
            <span v-if="scope.row.status === '1'">进入</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="time"
          label="进出时间"
          min-width="140"
        ></el-table-column>
        <el-table-column prop="type" label="车辆类型" min-width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.type === '0'">固定车辆</span>
            <span v-if="scope.row.type === '1'">临时车辆</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <!--添加 模态框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form
          :model="editOutInfoForm"
          ref="editOutInfoForm"
          :rules="caroutInfoRules"
          label-width="120px"
          :inline="true"
        >
          <el-form-item label="车牌号:">
            <el-input v-model="editOutInfoForm.carNumber"></el-input>
          </el-form-item>
           <el-form-item label="车辆类型:">
          <el-select v-model="editOutInfoForm.type" clearable @change="checkPark">
            <el-option label="固定车辆" value="0"> </el-option>
            <el-option label="临时车辆" value="1"> </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="车位号:">
             <el-select v-model="editOutInfoForm.parkNumber" placeholder="请选择">
              <el-option
                v-for="item in parkData"
                :key="item.id"
                :label="item.parkNumber"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
         
        <el-form-item label="出入状态:">
          <el-select v-model="editOutInfoForm.status" clearable>
            <el-option label="外出" value="0"> </el-option>
            <el-option label="进入" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登记时间:">
          <el-input v-model="gettime" disabled></el-input>
        </el-form-item>
        <el-form-item label="备注:">
            <el-input v-model="editOutInfoForm.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="submit"
           
            >提 交</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import webservice from "../../utils/webservice";
export default {
  data() {
    return {
      searchForm: {
        time:['','']
      },
      tableData: [],
      dialogFormVisible: false, //添加编辑的模态框标识
      editOutInfoForm: {
      }, //编辑车辆信息的表单
      // 默认显示第几页
      currentPage: 1,
      // 个数选择器（可修改）
      pageSizes: [1, 5, 10, 20],
      // 默认每页显示的条数（可修改）
      PageSize: 5,
      dialogTitle: "", //模态框标题
      caroutInfoRules: {
        carNumber: [
          { required: true, message: "请输入车牌号", trigger: "blur" },
        ],
      },
      gettime: "", //当前时间
      parkData:[],
      userInfo:{}
    };
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.getTime();
    this.getData()
  },
  methods: {
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      console.log(this.pageSizes);
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
    },
    //获取出入信息
    getData() {
       this.$refs['searchForm'].validate((valid) => {
         if(valid){
           let _param = {
        carNumber: this.searchForm.carNumber,
        status: this.searchForm.status,
        timeStart: this.searchForm.time[0] || undefined,
        timeEnd:this.searchForm.time[1] || undefined,
      };
      webservice.HttpPost("/api/record/findRecordListInfo", _param, (data) => {
        if (data.code === 200) {
          this.tableData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
         }else {
                    return false;
                }
       })
      
    },
    //获取当前时间
    getTime: function () {
      var _this = this;
      let yy = new Date().getFullYear();
      var mm =
        new Date().getMonth() < 10
          ? "0" + (new Date().getMonth() + 1)
          : new Date().getMonth() + 1;
      var dd =
        new Date().getDate() < 10
          ? "0" + new Date().getDate()
          : new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      _this.gettime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
    },
    //添加出入信息时判断所填车位
    checkPark() {
      let _param = {
        carNumber: this.editOutInfoForm.carNumber,
        status:'1',//未占用
        type:this.editOutInfoForm.type,
      };
      webservice.HttpPost("/api/park/findParkListInfo", _param, (data) => {
        if (data.code === 200) {
          this.parkData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //添加出入信息
    submit() {
          if(this.parkData.length){
            let _param = {
            carNumber: this.editOutInfoForm.carNumber,
            type: this.editOutInfoForm.type,
            time: this.gettime,
            status: this.editOutInfoForm.status,
            remark:this.editOutInfoForm.remark,
            parkId:this.editOutInfoForm.parkNumber
          };
          webservice.HttpPost("/api/record/addRecordInfo", _param, (data) => {
            if (data.code === 200) {
              this.$message.success({
                message: "添加成功！",
                showClose: true,
                duration: 1000,
              });
              this.dialogFormVisible = false;
              this.getData();
            } else {
              this.$message.error({
                message: data.message,
                showClose: true,
                duration: 1000,
              });
            }
          });
          }else{
            this.$message.error({
                message: '请输入正确的车位',
                showClose: true,
                duration: 1000,
              });
          }
    },
    //打开添加/编辑 模态框
    openDialog(flag, row) {
      this.dialogTitle = flag;
      this.dialogFormVisible = true;
    },
    //删除车辆信息
    deleteCarInfo(row) {
      let _param = {
        carId: row.id,
      };
      webservice.HttpPost("/api/car/deleteCarInfo", _param, (data) => {
        if (data.code === 200) {
          this.getData();
          this.$message.success({
            message: "删除成功！",
            showClose: true,
            duration: 1000,
          });
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>

<style>
</style>